package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCDemo1 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // 加载数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 创建数据库连接对象
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        // 创建SQL语句执行对象
        Statement statement = conn.createStatement();
        // 接收输入
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();// 用户名
        String password = scanner.nextLine();// 密码
        String nickname = scanner.nextLine();// 昵称
        Integer age = scanner.nextInt();// 年龄
        // 执行SQL语句
        String sql = "insert into userinfo(username,password,nickname,age) values('" + username + "','" + password + "','" + nickname + "'," + age + ")";
        int i = statement.executeUpdate(sql);
        // 断开数据库连接
        conn.close();
    }
}
